package com.ima;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.GridLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.ima.StateFrame;
import com.dev.CheckableItem;
import com.dev.FileBrowserTabMenu;
import com.dev.GUI;
import com.dev.IconView;
import com.dev.IconViewBlendMode;
import com.dev.IconViewClose;
import com.dev.IconViewPlusChar;
import com.dev.MainActivity;
import com.dev.R;
import com.dev.UnderlineSingleLineEditor;
import com.log;
import java.util.ArrayList;
import java.util.LinkedList;                                                                            
public class ImageManipulationPage extends LinearLayout implements OnClickListener,OnLongClickListener{
	MainActivity ma;
	float scaleMin=0.1f;
	float scaleMax;
	int imagebg;
	int layers_height;
	static int dialogHorItemHeight;
	int foregroundColor=0xff7f7f7f;
	int backgroundColor;
	public boolean editMode;
	public boolean canSave;
	public boolean canRefresh;
	public boolean growSeries=true;
	HorizontalScrollView titlebar_hsv;
	GridLayout tools;
	IconView ivMenu,ivShrink,ivBucket,ivRectSelect,ivTubular,ivAlign;
	IconView ivAdd,ivInside,ivMagic,ivSelection,ivEllipseSelect;
	IconView ivChartBar,ivPath,ivRuler,ivCompass,ivText,ivPencil;
	IconView ivNow,ivSettings,ivMouse,ivPick,ivDrag,ivLayer,ivLasso;
	IconView setCenter,setRight,setLeft,setAll,setXor,setNow;
	IconView ivPara,ivBlend;
	ColorRevealer colorReveal;
	LinearLayout layers;
	LinearLayout titlebar;
	LinearLayout titlebar_linear;
	LayoutParams titlebarlp;
	LayoutParams layers_lp;
	LocallySettings locallySettings;
	HorizontalScrollView layers_hsv;
	public LinkedList<StateFrame> undoable=new LinkedList<StateFrame>();
	public LinkedList<StateFrame> redoable=new LinkedList<StateFrame>();
	public CanvasView cv;
	QueryCreate queryCreate;
	QueryChart queryChart;
	QueryForBlend queryBlend;
	ActionMenu actionMenu;
	ArrayList<Layer> layerlist=new ArrayList<Layer>();
	ArrayList<Integer> verGuides=new ArrayList<Integer>();
	ArrayList<Object> objects=new ArrayList<Object>();
	Drawable toolSelected;
	TextView status;
	static QueryPalette queryPalette;
    public ImageManipulationPage(MainActivity ma,Bitmap image){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		ss=new SetSelected();
		try{
		if(!image.isMutable())image=image.copy(Bitmap.Config.ARGB_8888,true);
		}catch(Exception e){log.alert(e.toString());return;}
		toolSelected=new ColorDrawable(0x55999999);
		ivAdd=new IconViewPlusChar(ma);
		ivAdd.setOnClickListener(this);
		ivInside=new IconView(ma);
		ivInside.setImageResource(R.drawable.expand);
		ivInside.setOnClickListener(this);
		ivMenu=new IconView(ma);
		ivMenu.setOnClickListener(this);
		ivMenu.setImageResource(R.drawable.ic_dots_vertical);
		ivShrink=new IconView(ma);
		ivShrink.setOnClickListener(this);
		ivShrink.setImageResource(R.drawable.shrink);
		ivMagic=new IconView(ma);
		ivMagic.setOnClickListener(this);
		ivMagic.setOnLongClickListener(this);
		ivMagic.setImageResource(R.drawable.magic_wand);
		ivPath=new IconView(ma);
		ivPath.setImageResource(R.drawable.path);
		ivPath.setOnClickListener(this);
		ivRuler=new IconView(ma);
		ivRuler.setImageResource(R.drawable.ic_ruler);
		ivRuler.setOnClickListener(this);
		ivCompass=new IconView(ma);
		ivCompass.setOnClickListener(this);
		ivCompass.setImageResource(R.drawable.ic_compass);
		ivSettings=new IconView(ma);
		ivSettings.setOnClickListener(this);
		ivSettings.setImageResource(R.drawable.ic_settings_black);
		int padding=(int)(3*ma.dp);
		ivMagic.setPadding(padding,padding,padding,padding);
		ivBucket=new IconView(ma);
		ivBucket.setOnClickListener(this);
		ivBucket.setImageResource(R.drawable.ic_paint_bucket);
		ivBucket.setPadding(-padding,padding,-padding,-(int)(6*ma.dp));
		ivChartBar=new IconView(ma);
		ivChartBar.setOnClickListener(this);
		ivChartBar.setImageResource(R.drawable.ic_chart_bar);
		ivMouse=new IconView(ma);
		ivMouse.setImageResource(R.drawable.ic_mouse);
		ivMouse.setOnClickListener(this);
		ivMouse.setOnLongClickListener(this);
		ivPick=new IconView(ma);
		ivPick.setImageResource(R.drawable.ic_cursor);
		ivPick.setOnClickListener(this);
		ivSelection=new IconView(ma);
		ivSelection.setOnClickListener(this);
		ivSelection.setImageResource(R.drawable.selection_channel);
		ivRectSelect=new IconView(ma);
		ivRectSelect.setOnClickListener(this);
		ivRectSelect.setImageResource(R.drawable.ic_selection_drag);
		ivDrag=new IconView(ma);
		ivDrag.setOnClickListener(this);
		ivDrag.setImageResource(R.drawable.drag);
		ivDrag.setBackground(toolSelected);
		ivNow=ivDrag;
		ivTubular=new IconView(ma);
		ivTubular.setImageResource(R.drawable.tubular);
		ivTubular.setOnClickListener(this);
		ivEllipseSelect=new IconView(ma);
		ivEllipseSelect.setImageResource(R.drawable.ellipse_select);
		ivEllipseSelect.setOnClickListener(this);
		ivText=new IconView(ma);
		ivText.setImageResource(R.drawable.text);
		ivText.setOnClickListener(this);
		ivLayer=new IconView(ma);
		ivLayer.setImageResource(R.drawable.ic_layers);
		ivLayer.setOnClickListener(this);
		ivPencil=new IconView(ma);
		ivPencil.setOnClickListener(this);
		ivPencil.setImageResource(R.drawable.ic_pencil);
		ivLasso=new IconView(ma);
		ivLasso.setOnClickListener(this);
		ivLasso.setImageResource(R.drawable.ic_lasso);
		ivAlign=new IconView(ma);
		ivAlign.setImageResource(R.drawable.align_left);
		ivAlign.setOnClickListener(this);
		ivPara=new IconView(ma);
		ivPara.setOnClickListener(this);
		ivPara.setImageResource(R.drawable.numbers);
		ivBlend=new IconViewBlendMode(ma);
		ivBlend.setOnClickListener(this);
		setCenter=new IconView(ma);
		setCenter.setImageResource(R.drawable.ic_set_center);
		setCenter.setOnClickListener(this);
		setXor=new IconView(ma);
		setXor.setOnClickListener(this);
		setXor.setImageResource(R.drawable.ic_set_left_right);
		setAll=new IconView(ma);
		setAll.setImageResource(R.drawable.ic_set_all);
		setAll.setOnClickListener(this);
		setLeft=new IconView(ma);
		setLeft.setOnClickListener(this);
		setLeft.setImageResource(R.drawable.ic_set_left);
		setRight=new IconView(ma);
		setRight.setOnClickListener(this);
		setRight.setImageResource(R.drawable.ic_set_right);
		titlebar_linear=new LinearLayout(ma);
		titlebar_linear.setOrientation(HORIZONTAL);
		titlebar_linear.addView(ivPick);
		titlebar_linear.addView(ivRuler,-2,-1);
		titlebar_linear.addView(ivCompass,-2,-1);
		titlebar_linear.addView(ivPath,-2,-1);
		titlebar_linear.addView(ivChartBar,-2,-1);
		titlebar_linear.addView(ivInside,-2,-1);
		titlebar_linear.addView(ivShrink,-2,-1);
		titlebar_hsv=new HorizontalScrollView(ma);
		titlebar_hsv.addView(titlebar_linear,-2,-1);
		titlebar_hsv.setHorizontalScrollBarEnabled(false);
		titlebar_hsv.setFillViewport(true);
		titlebar=new LinearLayout(ma);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(ivAdd);
		titlebar.addView(titlebar_hsv,new LayoutParams(0,-1,1f));
		titlebar.addView(ivMouse);
		titlebar.addView(ivMenu);
		titlebar.addView(ivSettings);
		titlebarlp=new LayoutParams(-1,(int)(27*ma.dp));
		padding=titlebarlp.height/10;
		ivDrag.setPadding(0,padding,0,padding);
		colorReveal=new ColorRevealer();
		LinearLayout sub1=new LinearLayout(ma);
		sub1.setOrientation(HORIZONTAL);
		sub1.addView(ivDrag);
		sub1.addView(ivSelection);
		sub1.addView(ivRectSelect);
		sub1.addView(ivTubular);
		sub1.addView(ivMagic);
		sub1.addView(ivBucket);
		sub1.addView(ivEllipseSelect);
		sub1.addView(ivText);
		LinearLayout sub2=new LinearLayout(ma);
		sub2.setOrientation(HORIZONTAL);
		sub2.addView(ivLayer);
		sub2.addView(ivPencil);
		sub2.addView(ivLasso);
		sub2.addView(ivAlign);
		sub2.addView(ivPara);
		sub2.addView(ivBlend);
		LinearLayout sub3=new LinearLayout(ma);
		sub3.setOrientation(HORIZONTAL);
		sub3.addView(setAll);
		sub3.addView(setXor);
		sub3.addView(setLeft);
		sub3.addView(setRight);
		sub3.addView(setCenter);
		LinearLayout right=new LinearLayout(ma);
		right.setOrientation(VERTICAL);
		right.addView(sub1,titlebarlp);
		right.addView(sub2,titlebarlp);
		right.addView(sub3,titlebarlp);
		LinearLayout options=new LinearLayout(ma);
		options.setOrientation(HORIZONTAL);
		options.addView(colorReveal);
		options.addView(right,new LayoutParams(0,-1,1f));
		status=new TextView(ma);
		status.setPadding(10,0,0,0);
		status.setTypeface(Typeface.MONOSPACE);
		status.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
		status.setHint("无提示");
		status.setTextColor(0xff000000);
		status.setSingleLine();
		status.setGravity(Gravity.CENTER_VERTICAL);
		cv=new CanvasView(this);
		cv.image=image;
		cv.imageW=image.getWidth();
		cv.imageH=image.getHeight();
		addView(titlebar,titlebarlp);
		addView(options,-1,titlebarlp.height*3);
		addView(status,-1,(int)(20*ma.dp));
		addView(cv,new LayoutParams(-1,0,1f));
		scaleMax=ma.dialogTitleBarHeight;
		dialogHorItemHeight=(int)(27*ma.dp);
	}
	public void redo(){
		if(redoable.isEmpty())return;
		StateFrame state=redoable.pop();
		if(state instanceof SelectionFrame){
			SelectionFrame sf=(SelectionFrame)state;
			SelectionFrame willpush=new SelectionFrame();
			willpush.selected=cv.selected;
			cv.selected=sf.selected;
			makeMask(sf.selected);
			status.setText("已重做选区更改");
			invalidate();
			undoable.push(willpush);
			ma.gui.ivUndo.setColorFilter(null);
			ma.gui.ivUndo.invalidate();
			if(redoable.isEmpty()){
				ma.gui.ivRedo.setColorFilter(IconView.unable);
				ma.gui.ivRedo.invalidate();
			}
		}else if(state instanceof ImageFrame){
			ImageFrame ii=(ImageFrame)state;
			ImageFrame willpush=new ImageFrame();
			willpush.image=cv.image;
			cv.image=ii.image;
			invalidate();
			cv.imp.status.setText("已重做图像更改");
			undoable.push(willpush);
			ma.gui.ivUndo.setColorFilter(null);
			ma.gui.ivUndo.invalidate();
			if(redoable.isEmpty()){
				ma.gui.ivRedo.setColorFilter(IconView.unable);
				ma.gui.ivRedo.invalidate();
			}
		}
	}
	public void undo(){
		if(undoable.isEmpty())return;
		StateFrame state=undoable.pop();
		if(state instanceof SelectionFrame){
			SelectionFrame poped=(SelectionFrame)state;
			SelectionFrame sf=new SelectionFrame();
			sf.selected=cv.selected;
			cv.selected=poped.selected;
			makeMask(poped.selected);
			invalidate();
			cv.imp.status.setText("已撤销选区更改");
			redoable.push(sf);
			ma.gui.ivRedo.setColorFilter(null);
			ma.gui.ivRedo.invalidate();
			if(undoable.isEmpty()){
				ma.gui.ivUndo.setColorFilter(IconView.unable);
				ma.gui.ivUndo.invalidate();
			}
		}else if(state instanceof ImageFrame){
			ImageFrame ii=(ImageFrame)state;
			ImageFrame iii=new ImageFrame();
			iii.image=cv.image;
			cv.image=ii.image;
			invalidate();
			cv.imp.status.setText("已撤销图像更改");
			redoable.push(iii);
			ma.gui.ivRedo.setColorFilter(null);
			ma.gui.ivRedo.invalidate();
			if(undoable.isEmpty()){
				ma.gui.ivUndo.setColorFilter(IconView.unable);
				ma.gui.ivUndo.invalidate();
			}

		}
	}
	void makeMask(byte[] selected){
		int w=cv.iw;
		int h=cv.ih;
		//int count=0;
		for(int x=0;x<w;x++){
			for(int y=0;y<h;y++){
				int b=(0|selected[x+w*y])>>>24;
				//int colorbits=255-b;
				int px=b<<24;
				px&=0xff000000;//colorbits<<16+colorbits<<8+colorbits;
				//count=px;
				cv.imageMask.setPixel(x,y,px);
			}
		}
		//log.alert(String.format("%08x",count));
	}
	@Override
	public void onClick(View view) {
		if(view==ivInside){//onClick
			int vw=cv.getWidth();
			int vh=cv.getHeight();
			int iw=cv.image.getWidth();
			cv.imageH=cv.image.getHeight()*vw/iw;
			cv.imageW=vw;
			cv.scrollTo(0,-(vh-vw)/2);
			status.setText("scale:"+String.format("%.1f",vw/(float)iw));
		}else if(view==ivShrink){//onClick
			int vw=cv.getWidth();
			int vh=cv.getHeight();
			cv.imageW=cv.image.getWidth();
			cv.imageH=cv.image.getHeight();
			int a=-(vw-cv.imageW)/2;
			int b=-(vh-cv.imageH)/2;
			cv.scrollTo(a,b);
			status.setText("scale:1.0");
		}else if(view==ivMagic){//onClick
			final String hint1="魔棒工具";
			if(ivNow==null){
				ivMagic.setBackground(toolSelected);
				ivNow=ivMagic;
				status.setText(hint1);
				if(cv.imageMask==null){
					cv.imageMask=Bitmap.createBitmap(cv.image.getWidth(),cv.image.getHeight(),Bitmap.Config.ARGB_8888);
				}
			}else if(ivNow==ivMagic){
				if(queryForMagic==null)queryForMagic=new QueryForMagic(ma);
				queryForMagic.show(this);
			}else{
				ivNow.setBackground(null);
				ivNow=ivMagic;
				ivMagic.setBackground(toolSelected);
				status.setText(hint1);
				if(cv.imageMask==null){
					cv.imageMask=Bitmap.createBitmap(cv.image.getWidth(),cv.image.getHeight(),Bitmap.Config.ARGB_8888);
				}
			}
		}else if(view==ivAdd){//onClick
			if(queryCreate==null)queryCreate=new QueryCreate();
			queryCreate.show();
		}else if(view==ivBucket){//onClick
			final String hint1="油漆桶工具";
			if(ivNow==null){
				ivBucket.setBackground(toolSelected);
				ivNow=ivBucket;
				status.setText(hint1);
			}else if(ivNow==ivBucket){
				if(queryForBucket==null)queryForBucket=new QueryForBucket();
				queryForBucket.show();
			}else{
				ivNow.setBackground(null);
				ivNow=ivBucket;
				ivBucket.setBackground(toolSelected);
				status.setText(hint1);
			}
		}else if(view==ivPath){//onClick
			final String hint1="路径工具";
			if(ivNow==null){
				ivPath.setBackground(toolSelected);
				ivNow=ivPath;
				status.setText(hint1);
			}else if(ivNow==ivPath){
				if(queryForPen==null)queryForPen=new QueryForPen();
				queryForPen.show();
			}else{
				ivNow.setBackground(null);
				ivNow=ivPath;
				ivPath.setBackground(toolSelected);
				status.setText(hint1);
			}
		}else if(view==ivRuler){//onClick
			final String hint1="直尺工具";
			final String hint2="取消直尺工具";
			if(ivNow==null){
				ivRuler.setBackground(toolSelected);
				ivNow=ivRuler;
				status.setText(hint1);
			}else if(ivNow==ivRuler){
				ivNow=null;
				ivRuler.setBackground(null);
				status.setText(hint2);
			}else{
				ivNow.setBackground(null);
				ivNow=ivRuler;
				ivRuler.setBackground(toolSelected);
				status.setText(hint1);
			}
		}else if(view==ivCompass){//onClick
			final String hint1="角度工具";
			final String hint2="取消角度工具";
			if(ivNow==null){
				ivCompass.setBackground(toolSelected);
				ivNow=ivCompass;
				status.setText(hint1);
			}else if(ivNow==ivCompass){
				ivNow=null;
				ivCompass.setBackground(null);
				status.setText(hint2);
			}else{
				ivNow.setBackground(null);
				ivNow=ivCompass;
				ivCompass.setBackground(toolSelected);
				status.setText(hint1);
			}
		}else if(view==ivSettings){//onClick
			if(locallySettings==null)locallySettings=new LocallySettings();
			locallySettings.show();
		}else if(view==ivChartBar){//onClick
			if(ivNow==null){
				if(chart==0){
					if(queryChart==null)queryChart=new QueryChart();
					queryChart.show();
				}else{
					ivChartBar.setBackground(toolSelected);
					ivNow=ivChartBar;
				}

			}else if(ivNow==ivChartBar){
				ivNow=null;
				ivChartBar.setBackground(null);
			}else if(chart==0){
				if(queryChart==null)queryChart=new QueryChart();
				queryChart.show();
			}else{
				ivNow.setBackground(null);
				ivNow=ivChartBar;
				ivChartBar.setBackground(toolSelected);
			}
		}else if(view==ivMenu){//onClick
			if(actionMenu==null)actionMenu=new ActionMenu();
			actionMenu.show();
		}else if(view==ivMouse){//onClick
			if(ivMouse.isSelected()){
				ivMouse.setBackground(null);
				ivMouse.setSelected(false);
				cv.invalidate();
			}else{
				ivMouse.setBackground(toolSelected);
				ivMouse.setSelected(true);
				cv.invalidate();
			}
		}else if(view==ivDrag){//onClick
			final String hint="抓手工具";
			if(ivNow==null){
				ivNow=ivDrag;
				ivDrag.setBackground(toolSelected);
			}else if(ivNow==ivDrag){
				ivNow=null;
				ivDrag.setBackground(null);
				status.setText(hint);
			}else{
				ivNow.setBackground(null);
				ivNow=ivDrag;
				ivDrag.setBackground(toolSelected);
				status.setText(hint);
			}
		}else if(view==ivTubular){//onClick
			final String hint="吸管工具";
			if(ivNow==null){
				ivNow=ivTubular;
				ivTubular.setBackground(toolSelected);
				status.setText(hint);
			}else if(ivNow==ivTubular){
				ivNow=null;
				ivTubular.setBackground(null);
				status.setText("取消吸管工具");
			}else{
				ivNow.setBackground(null);
				ivNow=ivTubular;
				ivTubular.setBackground(toolSelected);
				status.setText(hint);
			}
		}else if(view==ivPick){//onClick
			final String hint="拖动出矩形来选择画布上的对象";
			if(ivNow==null){
				ivNow=ivPick;
				ivPick.setBackground(toolSelected);
				status.setText(hint);
				cv.invalidate();
			}else if(ivNow==ivPick){
				if(queryForPick==null)queryForPick=new QueryForPick();
				queryForPick.show();
			}else{
				ivNow.setBackground(null);
				ivNow=ivPick;
				ivPick.setBackground(toolSelected);
				status.setText(hint);
				cv.invalidate();
			}
		}else if(view==ivRectSelect){//onClick
			final String hint1="距形选择工具";
			if(ivNow==null){
				ivNow=ivRectSelect;
				ivRectSelect.setBackground(toolSelected);
				status.setText(hint1);
				cv.invalidate();
			}else if(ivNow==ivRectSelect){
				if(qfrst==null)qfrst=new QueryForRectSelectTool();
				qfrst.show();
				//if(querySelectionTools==null)querySelectionTools=new QuerySelectionTools();
				//querySelectionTools.show(ivRectSelect);
			}else{
				ivNow.setBackground(null);
				ivNow=ivRectSelect;
				ivRectSelect.setBackground(toolSelected);
				status.setText(hint1);
				cv.invalidate();
			}
		}else if(view==ivEllipseSelect){//onClick
			final String hint1="椭圆选择工具";
			if(ivNow==null){
				ivNow=ivEllipseSelect;
				ivEllipseSelect.setBackground(toolSelected);
				status.setText(hint1);
				cv.invalidate();
			}else if(ivNow==ivEllipseSelect){
				if(qfest==null)qfest=new QueryForEllipSelectTool();
				qfest.show();
			}else{
				ivNow.setBackground(null);
				ivNow=ivEllipseSelect;
				ivEllipseSelect.setBackground(toolSelected);
				status.setText(hint1);
				cv.invalidate();
			}
		}else if(view==setAll){//onClick
			final String hint="联集";
			if(setNow==null){
				setNow=setAll;
				setAll.setBackground(ss);
				status.setText(hint);
			}else if(setNow==setAll){
				setNow=null;
				setAll.setBackground(null);
			}else{
				setNow.setBackground(null);
				setNow=setAll;
				setAll.setBackground(ss);
				status.setText(hint);
			}
		}else if(view==setXor){
			if(setNow==null){
				setNow=setXor;
				setXor.setBackground(ss);
				status.setText("差集");
			}else if(setNow==setXor){
				setNow=null;
				setXor.setBackground(null);
			}else{
				setNow.setBackground(null);
				setNow=setXor;
				setXor.setBackground(ss);
			}
		}else if(view==setLeft){
			if(setNow==null){
				setNow=setLeft;
				setLeft.setBackground(ss);
				status.setText("去除上部");
			}else if(setNow==setLeft){
				setNow=null;
				setLeft.setBackground(null);
			}else{
				setNow.setBackground(null);
				setNow=setLeft;
				setLeft.setBackground(ss);
			}
		}else if(view==setRight){
			if(setNow==null){
				setNow=setRight;
				setRight.setBackground(ss);
				status.setText("去除下部");
			}else if(setNow==setRight){
				setNow=null;
				setRight.setBackground(null);
			}else{
				setNow.setBackground(null);
				setNow=setRight;
				setRight.setBackground(ss);
			}
		}else if(view==setCenter){
			if(setNow==null){
				setNow=setCenter;
				setCenter.setBackground(ss);
				status.setText("交集");
			}else if(setNow==setCenter){
				setNow=null;
				setCenter.setBackground(null);
			}else{
				setNow.setBackground(null);
				setNow=setCenter;
				setCenter.setBackground(ss);
			}
		}else if(view==ivText){
			final String hint="文字工具";
			if(ivNow==null){
				ivNow=ivText;
				ivText.setBackground(toolSelected);
				status.setText(hint);
			}else if(ivNow==ivText){
				if(queryPaint==null)queryPaint=new QueryPaint(ma);
				queryPaint.show();
			}else{
				ivNow.setBackground(null);
				ivNow=ivText;
				ivText.setBackground(toolSelected);
				status.setText(hint);
			}
		}else if(view==ivPencil){
			final String hint="铅笔工具";
			if(ivNow==null){
				ivNow=ivPencil;
				ivPencil.setBackground(toolSelected);
				status.setText(hint);
			}else if(ivNow==ivPencil){
				if(queryPaint==null)queryPaint=new QueryPaint(ma);
				queryPaint.show();
			}else{
				ivNow.setBackground(null);
				ivNow=ivPencil;
				ivPencil.setBackground(toolSelected);
				status.setText(hint);
			}
		}else if(view==ivLasso){
			final String hint="套索工具";
			if(ivNow==null){
				ivNow=ivLasso;
				ivLasso.setBackground(toolSelected);
				status.setText(hint);
			}else if(ivNow==ivLasso){

			}else{
				ivNow.setBackground(null);
				ivNow=ivLasso;
				ivLasso.setBackground(toolSelected);
				status.setText(hint);
			}
		}else if(view==ivPara){
			if(ivNow==null){
				Toast.makeText(ma,"no parameter can edit",0).show();
			}
		}else if(view==ivSelection){
			if(qfsc==null)qfsc=new QueryForSelectionChannel(ma);
			qfsc.show(this);
		}else if(view==ivBlend){
			if(queryBlend==null)queryBlend=new QueryForBlend(ma);
			queryBlend.show(this);
		}
	}
	@Override
	public boolean onLongClick(View view) {
		if(view==ivMouse){
			if(queryMouse==null)queryMouse=new QueryMouse();
			queryMouse.show();
			return true;
		}
		return false;
	}
	boolean inited;
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		int vw=getWidth();
		int iw=cv.image.getWidth();
		//int ih=image.getHeight();
		cv.imageWMax=(int)(iw*scaleMax);
		cv.imageWMin=(int)(iw*scaleMin);
		if(cv.imageWMax<vw){
			cv.imageWMax=vw;
		}
		if(!inited){
			if(ma.shr.getBoolean(MoreSettingForIMP.imgFillScreen,true)){
				int vh=cv.getHeight();
				cv.imageH=cv.image.getHeight()*vw/iw;
				cv.imageW=vw;
				cv.scrollTo(0,-(vh-vw)/2);
				status.setText("scale:"+String.format("%.1f",vw/(float)iw));
			}else{
				cv.imageW=iw;
				cv.imageH=cv.image.getHeight();
			}
			inited=true;
		}
	}
	QueryForPick queryForPick;
	class QueryForPick extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		GUI.Item itReverse;
		GUI.Item itAll;
		GUI.Item itCancel;
		QueryForPick(){
			super(ma);
			setOrientation(1);
			setBackground(new GUI.BorderDrawable());
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			itReverse=new GUI.Item(ma);
			itReverse.setText("反选");
			itReverse.setOnClickListener(this);
			itAll=new GUI.Item(ma);
			itAll.setText("全选");
			itAll.setOnClickListener(this);
			itCancel=new GUI.Item(ma);
			itCancel.setText("全不选");
			itCancel.setOnClickListener(this);
			LayoutParams itlp=new LayoutParams(-1,dialogHorItemHeight);
			addView(itReverse,itlp);
			addView(itAll,itlp);
			addView(itCancel,itlp);
		}
		void show(){
			wlp.width=ma.wp/2;
			wlp.height=-2;
			Rect rect=new Rect();
			ivPick.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	EditBar editbar;
	class EditBar extends LinearLayout implements OnClickListener{
		IconView quit;
		EditBar(){
			super(ma);
			setOrientation(HORIZONTAL);
			setBackgroundColor(0xffffffff);
			quit=new IconView(ma);
			quit.setImageResource(R.drawable.ic_quit_thin);
			quit.setOnClickListener(this);
		}
		void show(){
			ma.gui.removeViewAt(0);
			ma.gui.addView(this,0,ma.gui.titlebarlp);
		}
		void dismiss(){
			ma.gui.removeViewAt(0);
			ma.gui.addView(ma.gui.titlebar,0,ma.gui.titlebarlp);
		}
		@Override
		public void onClick(View view) {
			if(view==quit){
				dismiss();
			}
		}
	}
	QueryForSelectionChannel qfsc;
	QueryMouse queryMouse;
	class QueryMouse extends LinearLayout implements OnClickListener,OnEditorActionListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		LinearLayout titlebar;
		TextView title;
		TextView row1_title;
		UnderlineSingleLineEditor row1_editor;
		IconViewClose close;
		LinearLayout row1;
		QueryMouse(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			close=new IconViewClose(ma);
			close.setOnClickListener(this);
			title=new TextView(ma);
			title.setText("鼠标样式");
			int padding=9;
			int textSize=(int)(13*ma.dp);
			title.setPadding(padding,0,0,0);
			title.setGravity(Gravity.CENTER_VERTICAL);
			title.getPaint().setTextSize(textSize);
			titlebar=new LinearLayout(ma);
			titlebar.setOrientation(HORIZONTAL);
			titlebar.setBackground(new GUI.BorderDrawable());
			titlebar.addView(title,new LayoutParams(0,-1,1f));
			titlebar.addView(close);
			row1_title=new TextView(ma);
			row1_title.setTextColor(0xff000000);
			row1_title.setPadding(padding,0,0,0);
			row1_title.setGravity(Gravity.CENTER_VERTICAL);
			row1_title.setText("鼠标光标颜色#");
			row1_editor=new UnderlineSingleLineEditor(ma);
			row1_editor.setPadding(padding,0,padding,0);
			row1_editor.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
			row1_editor.getPaint().setTextSize(textSize);
			row1_editor.setImeOptions(EditorInfo.IME_ACTION_DONE);
			row1_editor.setOnEditorActionListener(this);
			row1=new LinearLayout(ma);
			row1.setOrientation(HORIZONTAL);
			row1.addView(row1_title,-2,-1);
			row1.addView(row1_editor,new LayoutParams(0,-1,1f));
			addView(titlebar,-1,ma.dialogTitleBarHeight);
			addView(row1,-1,dialogHorItemHeight);

		}
		void show(){
			wlp.width=ma.wp*2/3;
			wlp.height=-2;
			ma.wm.addView(this,wlp);
		}

		@Override
		public boolean onEditorAction(TextView textView, int p, KeyEvent keyEvent) {
			return false;
		}
		@Override
		public void onClick(View view) {
			if(view==close){
				ma.wm.removeView(this);
			}
		}

		@Override
		protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
			return true;
		}
	}
	QueryForMagic queryForMagic;
	Paint paint=new Paint();
	QueryPaint queryPaint;
	SetSelected ss;
	class SetSelected extends ColorDrawable{
		int stroke;
		SetSelected(){
			stroke=(int)(3*ma.dp);
		}
		@Override 
		public void draw(Canvas canvas){
			int cw=canvas.getWidth();
			int ch=canvas.getHeight();
			Paint p=new Paint();
			p.setColor(0xff757575);
			canvas.drawRect(0,ch-stroke,cw,ch,p);
		}
	}
	QueryForPen queryForPen;
	class QueryForPen extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		GUI.Item itStrokePath;
		GUI.Item itFillPath;
		GUI.Item itConvert;
		QueryForPen(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			itStrokePath=new GUI.Item(ma);
			itStrokePath.setText("勾画路径");
			itStrokePath.setOnClickListener(this);
			itFillPath=new GUI.Item(ma);
			itFillPath.setText("涂色路径");
			itFillPath.setOnClickListener(this);
			itConvert=new GUI.Item(ma);
			itConvert.setText("转为选区");
			itConvert.setOnClickListener(this);
			LayoutParams itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itStrokePath,itlp);
			addView(itFillPath,itlp);
			addView(itConvert,itlp);
		}
		void show(){
			wlp.width=ma.wp/2;
			wlp.height=-2;
			Rect rect=new Rect();
			ivPath.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	QueryForBucket queryForBucket;
	class QueryForBucket extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		GUI.Item itFill;
		QueryForBucket(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			itFill=new GUI.Item(ma);
			itFill.setText("填充光标所在选区");
			LayoutParams rowlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itFill,rowlp);
		}
		void show(){
			wlp.width=ma.wp/2;
			wlp.height=-2;
			Rect rect=new Rect();
			ivBucket.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	class ColorRevealer extends View{
		int fingerMovedLim;
		ColorRevealer(){
			super(ma);
			fingerMovedLim=(int)(4*ma.dp);
		}
		Rect fg=new Rect();
		Rect bg=new Rect();
		int borderWidth;
		int padding;
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			int vw=getWidth();
			int vh=getHeight();
			padding=9;
			borderWidth=4;
			int out=2;
			int colorWidth=vh*8/15;
			fg.left=padding+borderWidth;
			fg.top=fg.left;
			fg.right=fg.left+colorWidth;
			fg.bottom=fg.top+colorWidth;
			bg.right=vw-padding-borderWidth;
			bg.bottom=vh-padding-borderWidth;
			bg.left=bg.right-colorWidth;
			bg.top=bg.bottom-colorWidth;
			Paint p=new Paint();
			canvas.save();
			canvas.clipRect(bg);
			int zebraWidth=6;
			int start=zebraWidth/2;
			p.setColor(0xff000000);
			for(int x=bg.left-start;x<bg.right;x+=zebraWidth){
				for(int y=bg.top-start;y<bg.bottom;y+=zebraWidth*2){
					canvas.drawRect(x,y,x+zebraWidth,y+zebraWidth,p);
				}
				x+=zebraWidth;
				for(int y=bg.top-start+zebraWidth;y<bg.bottom;y+=zebraWidth*2){
					canvas.drawRect(x,y,x+zebraWidth,y+zebraWidth,p);
				}
			}
			canvas.restore();
			int rr=bg.right+borderWidth,bb=bg.bottom+borderWidth;
			int ll=bg.left-borderWidth,tt=bg.top-borderWidth;
			canvas.drawRect(ll,tt,ll+out,bb,p);
			canvas.drawRect(ll,tt,rr,tt+out,p);
			canvas.drawRect(rr-out,tt,rr,bb,p);
			canvas.drawRect(ll,bb-out,rr,bb,p);
			p.setColor(0xffffffff);
			canvas.drawRect(ll+out,tt+out,bg.left,bb-out,p);
			canvas.drawRect(ll+out,tt+out,rr-out,bg.top,p);
			canvas.drawRect(bg.right,tt+out,rr-out,bb-out,p);
			canvas.drawRect(ll+out,bg.bottom,rr-out,bb-out,p);
			p.setColor(backgroundColor);
			canvas.drawRect(bg,p);
			canvas.save();
			canvas.clipRect(fg);
			canvas.drawColor(0xffffffff);
			p.setColor(0xff000000);
			for(int x=fg.left-start;x<fg.right;x+=zebraWidth){
				for(int y=fg.top-start;y<fg.bottom;y+=zebraWidth*2){
					canvas.drawRect(x,y,x+zebraWidth,y+zebraWidth,p);
				}
				x+=zebraWidth;
				for(int y=fg.top-start+zebraWidth;y<fg.bottom;y+=zebraWidth*2){
					canvas.drawRect(x,y,x+zebraWidth,y+zebraWidth,p);
				}
			}
			p.setColor(foregroundColor);
			canvas.restore();
			canvas.drawRect(fg,p);
			p.setColor(0xff000000);
			ll=fg.left-borderWidth;
			tt=fg.top-borderWidth;
			rr=fg.right+borderWidth;
			bb=fg.bottom+borderWidth;
			canvas.drawRect(ll,tt,ll+out,bb,p);
			canvas.drawRect(ll,tt,rr,tt+out,p);
			canvas.drawRect(rr-out,tt,rr,bb,p);
			canvas.drawRect(ll,bb-out,rr,bb,p);
			p.setColor(0xffffffff);
			canvas.drawRect(ll+out,tt+out,fg.left,bb-out,p);
			canvas.drawRect(ll+out,tt+out,rr-out,fg.top,p);
			canvas.drawRect(fg.right,tt+out,rr-out,bb-out,p);
			canvas.drawRect(ll+out,fg.bottom,rr-out,bb-out,p);
			int distance=4;
			rr=bg.left-borderWidth-distance;
			bb=vh-padding;
			int ww=(bg.left-borderWidth*2-distance)*8/15;
			ll=rr-ww;
			tt=bb-ww;
			canvas.drawRect(ll+1,tt+1,rr-1,bb-1,p);
			p.setColor(0xff000000);//draw a black small rect
			canvas.drawRect(ll,tt,ll+1,bb,p);
			canvas.drawRect(ll,tt,rr,tt+1,p);
			canvas.drawRect(rr-1,tt,rr,bb,p);
			canvas.drawRect(ll,bb-1,rr,bb,p);
			tt=fg.bottom+borderWidth+distance;
			ll=padding;
			rr=ll+ww;
			bb=tt+ww;
			canvas.drawRect(ll,tt,rr,bb,p);
			distance=9;
			int space=vh-bg.height()-padding*2-borderWidth*2;
			int pillarRadius=space/14;
			int capRadius=pillarRadius*5/2;
			ll=fg.right+borderWidth+distance;
			bb=bg.top-borderWidth-distance;
			tt=space/4;
			rr=vw-tt;
			int caplen=vw/10;
			Path path=new Path();
			path.moveTo(ll,tt+capRadius);
			path.lineTo(ll+caplen,tt);
			path.lineTo(ll+caplen,tt+capRadius-pillarRadius);
			path.lineTo(rr-capRadius+pillarRadius,tt+capRadius-pillarRadius);
			path.lineTo(rr-capRadius+pillarRadius,bb-caplen);
			path.lineTo(rr,bb-caplen);
			path.lineTo(rr-capRadius,bb);
			path.lineTo(rr-capRadius*2,bb-caplen);
			path.lineTo(rr-capRadius-pillarRadius,bb-caplen);
			path.lineTo(rr-capRadius-pillarRadius,tt+capRadius+pillarRadius);
			path.lineTo(ll+caplen,tt+capRadius+pillarRadius);
			path.lineTo(ll+caplen,tt+capRadius*2);
			path.close();
			p.setStyle(Paint.Style.STROKE);
			p.setStrokeWidth(3);
			canvas.drawPath(path,p);
		}
		Point touchDown=new Point();
		boolean fingerMoved;
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			int ex=(int)event.getX();
			int ey=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=ex;
					touchDown.y=ey;
					fingerMoved=false;
					break;
				case MotionEvent.ACTION_MOVE:
					if(fingerMoved){

					}else{
						int dx=ex-touchDown.x;
						int dy=ey-touchDown.y;
						int dx_abs=Math.abs(dx);
						int dy_abs=Math.abs(dy);
						if(dx_abs>fingerMovedLim||dy_abs>fingerMovedLim){
							fingerMoved=true;
						}
					}
					break;
				case MotionEvent.ACTION_UP:
					if(fingerMoved){

					}else{
						if(fg.contains(ex,ey)){
							if(queryPalette==null)queryPalette=new QueryPalette(ma);
							queryPalette.show(ImageManipulationPage.this,0);
						}else if(bg.contains(ex,ey)){
							if(queryPalette==null)queryPalette=new QueryPalette(ma);
							queryPalette.show(ImageManipulationPage.this,1);
						}else if(ex>=fg.right&&ey<bg.top){
							int i=foregroundColor;
							foregroundColor=backgroundColor;
							backgroundColor=i;
							invalidate();
						}else if(ex<bg.left&&ey>=fg.bottom){
							foregroundColor=0xff000000;
							backgroundColor=0xffffffff;
							invalidate();
						}
					}
					break;
				default:
					fingerMoved=false;
			}
			return true;
		}
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			int h=MeasureSpec.getSize(heightMeasureSpec);
			setMeasuredDimension(h,h);
		}
	}
	QueryForRectSelectTool qfrst;
	class QueryForRectSelectTool extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		GUI.Item itBuild;
		GUI.Item itShrink;
		GUI.Item itDiscard;
		GUI.Item itEarse;
		GUI.Item itFill;
		CheckableItem itSquare;
		QueryForRectSelectTool(){
			super(ma);
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			itBuild=new GUI.Item(ma);
			itBuild.setText("构建选区");
			itBuild.setOnClickListener(this);
			itShrink=new GUI.Item(ma);
			itShrink.setText("收缩选框");
			itShrink.setOnClickListener(this);
			itDiscard=new GUI.Item(ma);
			itDiscard.setText("放弃选择");
			itDiscard.setOnClickListener(this);
			itSquare=new CheckableItem(ma,"正方形选择");
			itSquare.setOnClickListener(this);
			itEarse=new GUI.Item(ma);
			itEarse.setText("擦除选框");
			itEarse.setOnClickListener(this);
			itFill=new GUI.Item(ma);
			itFill.setText("涂色选框");
			itFill.setOnClickListener(this);
			LayoutParams itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itBuild,itlp);
			addView(itShrink,itlp);
			addView(itDiscard,itlp);
			addView(itSquare,itlp);
			addView(itEarse,itlp);
			addView(itFill,itlp);
		}
		void show(){
			wlp.width=ma.wp/2;
			wlp.height=-2;
			Rect rect=new Rect();
			ivRectSelect.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
			if(view==itBuild){//onClick
				if(cv.rectSelection==null){
					Toast.makeText(ma,"当前没有创建矩形选框",0).show();
				}else{
					int iw=cv.image.getWidth();
					int ih=cv.image.getHeight();
					int ll=cv.rectSelection.left;
					int tt=cv.rectSelection.top;
					int rr=cv.rectSelection.right;
					int bb=cv.rectSelection.bottom;
					int i;
					if(ll>rr){
						i=ll;
						ll=rr;
						rr=i;
					}
					if(tt>bb){
						i=tt;
						tt=bb;
						bb=i;
					}
					if(rr<=iw&&bb<=ih){
						if(ll<0)ll=0;
						if(tt<0)tt=0;
						if(cv.imageMask==null)cv.imageMask=Bitmap.createBitmap(iw,ih,Bitmap.Config.ARGB_8888);
						for(int x=ll;x<rr;x++){
							//?????
							//?????
						}
						cv.rectSelection=null;
						cv.invalidate();
					}else{
						cv.rectSelection=null;
						cv.invalidate();
					}
				}
			}else if(view==itDiscard){//onClick
				cv.rectSelection=null;
				cv.invalidate();
			}else if(view==itEarse){//onClick
				if(cv.rectSelection==null){
					Toast.makeText(ma,"当前没有创建矩形选框",0).show();
				}else{
					cv.atImageChange();
					Canvas canvas=new Canvas(cv.image);
					canvas.clipRect(cv.rectSelection.left,cv.rectSelection.top,cv.rectSelection.right,cv.rectSelection.bottom);
					canvas.drawColor(backgroundColor,PorterDuff.Mode.SRC);
					cv.invalidate();
					ma.wm.removeView(this);
				}
			}else if(view==itFill){
				if(cv.rectSelection==null){
					Toast.makeText(ma,"当前没有创建矩形选框",0).show();
				}else{
					cv.atImageChange();
					Canvas canvas=new Canvas(cv.image);
					canvas.clipRect(cv.rectSelection.left,cv.rectSelection.top,cv.rectSelection.right,cv.rectSelection.bottom);
					canvas.drawColor(foregroundColor);
					cv.invalidate();
					ma.wm.removeView(this);
				}
			}
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	QueryForEllipSelectTool qfest;
	class QueryForEllipSelectTool extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		CheckableItem itFixCircle;
		GUI.Item itFeather;
		GUI.Item itShrink;
		GUI.Item itDiscard;
		GUI.Item itBuild;
		GUI.Item itEarse;
		GUI.Item itFill;
		LinearLayout row1;
		TextView row1_title;
		UnderlineSingleLineEditor row1_editor;
		QueryForEllipSelectTool(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			itFeather=new GUI.Item(ma);
			itFeather.setOnClickListener(this);
			itFixCircle=new CheckableItem(ma,"正圆选择");
			itFixCircle.setOnClickListener(this);
			itShrink=new GUI.Item(ma);
			itShrink.setText("收缩选框");
			itShrink.setOnClickListener(this);
			itDiscard=new GUI.Item(ma);
			itDiscard.setOnClickListener(this);
			itDiscard.setText("放弃选择");
			itBuild=new GUI.Item(ma);
			itBuild.setOnClickListener(this);
			itBuild.setText("构建选区");
			itEarse=new GUI.Item(ma);
			itEarse.setOnClickListener(this);
			itEarse.setText("擦除选框");
			itFill=new GUI.Item(ma);
			itFill.setOnClickListener(this);
			itFill.setText("涂色选框");
			LayoutParams itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itFixCircle,itlp);
			addView(itShrink,itlp);
			addView(itDiscard,itlp);
			addView(itBuild,itlp);
			addView(itEarse,itlp);
			addView(itFill,itlp);
		}
		void show(){
			itFixCircle.setSelected(regularCircleSelect);
			wlp.width=ma.wp/2;
			wlp.height=-2;
			Rect rect=new Rect();
			ivEllipseSelect.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
			if(view==itFixCircle){
				if(itFixCircle.isSelected()){
					itFixCircle.setSelected(false);
					regularCircleSelect=false;
				}else{
					itFixCircle.setSelected(true);
					regularCircleSelect=true;
				}
				ma.wm.removeView(this);
			}else if(view==itEarse){
				if(cv.ellip==null){
					Toast.makeText(ma,"请先创建椭圆选框",0).show();
				}else{
					cv.atImageChange();
					Canvas canvas=new Canvas(cv.image);
					Path oval=new Path();
					oval.addOval(cv.ellip.left,cv.ellip.top,cv.ellip.right,cv.ellip.bottom,Path.Direction.CW);
					canvas.clipPath(oval);
					canvas.drawColor(backgroundColor,PorterDuff.Mode.SRC);
					cv.invalidate();
					ma.wm.removeView(this);
				}
			}else if(view==itFill){
				if(cv.ellip==null){
					Toast.makeText(ma,"请先创建选框",0).show();
				}else{
					cv.atImageChange();
					Canvas canvas=new Canvas(cv.image);
					Path oval=new Path();
					oval.addOval(cv.ellip.left,cv.ellip.top,cv.ellip.right,cv.ellip.bottom,Path.Direction.CW);
					canvas.clipPath(oval);
					canvas.drawColor(foregroundColor);
					cv.invalidate();
					ma.wm.removeView(this);
				}
			}
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}

	boolean regularCircleSelect;
	class LayerTab extends ImageView{
		Bitmap image;
		LayerTab(){
			super(ma);
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			int h=MeasureSpec.getSize(heightMeasureSpec);
			setMeasuredDimension(h,h);
		}
	}
	WindowManager.LayoutParams dialoglp=new WindowManager.LayoutParams();
	class QueryCreate extends LinearLayout implements OnClickListener{
		FileBrowserTabMenu.Item itEmptyLayer;
		FileBrowserTabMenu.Item itImage;
		FileBrowserTabMenu.Item itHorGuide;
		FileBrowserTabMenu.Item itVerGuide;
		FileBrowserTabMenu.Item itClose;
		LayoutParams itlp;
		QueryNewGuide queryNewGuide;
		QueryCreate(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			itEmptyLayer=new FileBrowserTabMenu.Item(ma,"空白图层");
			itEmptyLayer.setOnClickListener(this);
			itImage=new FileBrowserTabMenu.Item(ma,"导入图像");
			itImage.setOnClickListener(this);
			itHorGuide=new FileBrowserTabMenu.Item(ma,"横向辅助线");
			itHorGuide.setOnClickListener(this);
			itVerGuide=new FileBrowserTabMenu.Item(ma,"纵向辅助线");
			itVerGuide.setOnClickListener(this);
			itClose=new FileBrowserTabMenu.Item(ma,"取消");
			itClose.setOnClickListener(this);
			itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itEmptyLayer,itlp);
			addView(itImage,itlp);
			addView(itHorGuide,itlp);
			addView(itVerGuide,itlp);
			addView(itClose,itlp);
		}
		void show(){
			dialoglp.width=ma.wp*3/4;
			dialoglp.height=-2;
			dialoglp.gravity=Gravity.TOP|Gravity.LEFT;
			dialoglp.flags=dialoglp.FLAG_LAYOUT_IN_SCREEN;
			dialoglp.format=PixelFormat.TRANSLUCENT;
			Rect visible=new Rect();
			ivAdd.getGlobalVisibleRect(visible);
			dialoglp.x=visible.left;
			dialoglp.y=visible.bottom;
			ma.wm.addView(this,dialoglp);
		}
		void dismiss(){
			ma.wm.removeView(this);
		}
		@Override 
		public void onClick(View view){
			if(view==itClose){
				dismiss();
			}else if(view==itHorGuide){
				if(queryNewGuide==null)queryNewGuide=new QueryNewGuide();
				queryNewGuide.hor=true;
				queryNewGuide.show();
			}else if(view==itVerGuide){
				if(queryNewGuide==null)queryNewGuide=new QueryNewGuide();
				queryNewGuide.hor=false;
				queryNewGuide.show();
			}
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						dismiss();
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	Paint actionPaint=new Paint();
	boolean representByAntline;
	class ActionMenu extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		GUI.Item itSelection;
		GUI.Item itExport;

		ActionMenu(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			itSelection=new GUI.Item(ma);
			itSelection.setText("选区");
			itSelection.setOnClickListener(this);
			itExport=new GUI.Item(ma);
			itExport.setText("导出");
			itExport.setOnClickListener(this);
			LayoutParams itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itSelection,itlp);
			addView(itExport,itlp);
		}
		void show(){
			wlp.width=ma.wp/2;
			wlp.height=-2;
			Rect rect=new Rect();
			ivMenu.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {

		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	static MoreSettingForIMP moreSettingsForIMP;
	boolean adsorbToGuide;
	class LocallySettings extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		LinearLayout row2;
		GUI.Item itMore,itGuideColor;
		CheckableItem itAdsorbToGuide;
		GUI.Item itCvsbg,title2;
		CheckableItem itAntline;
		CheckableItem itDither;
		CheckableItem itFilter;
		CheckableItem itAntialias;
		LocallySettings(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			wlp.gravity=Gravity.LEFT|Gravity.TOP;
			wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			final int padding=10;
			itCvsbg=new GUI.Item(ma);
			itCvsbg.setOnClickListener(this);
			itGuideColor=new GUI.Item(ma);
			itMore=new GUI.Item(ma);
			itMore.setText("更多");
			itMore.setOnClickListener(this);
			itAdsorbToGuide=new CheckableItem(ma,"吸附到参考线");
			itAdsorbToGuide.setOnClickListener(this);
			itAdsorbToGuide.borderWidth=GUI.borderWidth;
			itAntline=new CheckableItem(ma,"使用蚂蚁线表现选区");
			itAntline.setOnClickListener(this);
			itAntline.borderWidth=GUI.borderWidth;
			itDither=new CheckableItem(ma,"图像抖动算法");
			itDither.setOnClickListener(this);
			itDither.borderWidth=GUI.borderWidth;
			itFilter=new CheckableItem(ma,"双线性插值算法");
			itFilter.setOnClickListener(this);
			itFilter.borderWidth=GUI.borderWidth;
			itAntialias=new CheckableItem(ma,"抗锯齿");
			itAntialias.setOnClickListener(this);
			itAntialias.borderWidth=GUI.borderWidth;
			LayoutParams rowlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itGuideColor,rowlp);
			addView(itAdsorbToGuide,rowlp);
			addView(itMore,rowlp);
			addView(itAntline,rowlp);
			addView(itDither,rowlp);
			addView(itFilter,rowlp);
			addView(itAntialias,rowlp);
		}
		final static String s="画布背景：";
		void show(){
			itAntline.setSelected(representByAntline);
			itDither.setSelected(actionPaint.isDither());
			itFilter.setSelected(actionPaint.isFilterBitmap());
			itAntialias.setSelected(actionPaint.isAntiAlias());
			itGuideColor.setText("十字参考线颜色：#"+String.format("%08x",cv.wirePaint.getColor()));
			itAdsorbToGuide.setSelected(adsorbToGuide);
			wlp.width=ma.wp/2;
			wlp.height=-2;
			Rect rect=new Rect();
			ivSettings.getGlobalVisibleRect(rect);
			wlp.x=rect.left;
			wlp.y=rect.bottom;
			if(imagebg==0){
				itCvsbg.setText(s+"黑白格子");
			}else{
				itCvsbg.setText(s+"None");
			}

			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
			if(view==itMore){//onClick
				if(moreSettingsForIMP==null)moreSettingsForIMP=new MoreSettingForIMP(ma);
				moreSettingsForIMP.show();
			}else if(view==itAdsorbToGuide){
				if(itAdsorbToGuide.isSelected()){
					itAdsorbToGuide.setSelected(false);
					adsorbToGuide=false;
				}else{
					itAdsorbToGuide.setSelected(true);
					adsorbToGuide=true;
				}
			}else if(view==itAntline){
				if(itAntline.isSelected()){
					itAntline.setSelected(false);
					representByAntline=false;
				}else{
					itAntline.setSelected(true);
					representByAntline=true;
				}
			}else if(view==itDither){
				if(itDither.isSelected()){
					itDither.setSelected(false);
					actionPaint.setDither(false);
				}else{
					itDither.setSelected(true);
					actionPaint.setDither(true);
				}
			}else if(view==itFilter){
				if(itFilter.isSelected()){
					itFilter.setSelected(false);
					actionPaint.setFilterBitmap(false);
				}else{
					itFilter.setSelected(true);
					actionPaint.setFilterBitmap(true);
				}
			}
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
	class QueryNewGuide extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams(),unitlp=new WindowManager.LayoutParams();
		LinearLayout titlebar;
		LinearLayout row1;
		LinearLayout row2;
		LinearLayout queryUnit;
		IconViewClose close;
		TextView title;
		TextView hint1,hint2;
		TextView valueUnit;
		UnderlineSingleLineEditor editor;
		GUI.Item unitPx,unitPercent;
		boolean hor;
		byte unit;
		QueryNewGuide(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			close=new IconViewClose(ma);
			close.setOnClickListener(this);
			final int padding=10;
			title=new TextView(ma);
			title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			title.setPadding(padding,0,0,0);
			title.setGravity(Gravity.CENTER_VERTICAL);
			titlebar=new LinearLayout(ma);
			titlebar.setOrientation(HORIZONTAL);
			titlebar.setBackground(new GUI.BorderDrawable());
			titlebar.addView(title,new LayoutParams(0,-1,1f));
			titlebar.addView(close);
			hint1=new TextView(ma);
			hint2=new TextView(ma);
			hint1.setGravity(Gravity.CENTER_VERTICAL);
			hint2.setGravity(Gravity.CENTER_VERTICAL);
			hint1.setPadding(padding,0,0,0);
			hint2.setPadding(padding,0,0,0);
			hint1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			hint2.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			hint1.setText("位置：");
			hint2.setText("数值单位：");
			valueUnit=new TextView(ma);
			valueUnit.setGravity(Gravity.CENTER);
			valueUnit.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
			valueUnit.setOnClickListener(this);
			editor=new UnderlineSingleLineEditor(ma);
			editor.setPadding(0,0,padding,0);
			row1=new LinearLayout(ma);
			row2=new LinearLayout(ma);
			row1.setOrientation(HORIZONTAL);
			row2.setOrientation(HORIZONTAL);
			row1.addView(hint1,-2,-1);
			row1.addView(editor,new LayoutParams(0,-1,1f));
			row2.addView(hint2,-2,-1);
			row2.addView(valueUnit,new LayoutParams(0,-1,1f));
			LayoutParams titlebarlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			LayoutParams rowlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(titlebar,titlebarlp);
			addView(row1,rowlp);
			addView(row2,rowlp);
			queryUnit=new LinearLayout(ma){
				Point touchDown=new Point();
				@Override
				public boolean onTouchEvent(MotionEvent event){
					int x=(int)event.getX();
					int y=(int)event.getY();
					switch(event.getAction()){
						case MotionEvent.ACTION_DOWN:
							touchDown.x=x;
							touchDown.y=y;
							break;
						case MotionEvent.ACTION_UP:
							Rect visible=new Rect();
							getGlobalVisibleRect(visible);
							int[] is=new int[2];
							getLocationOnScreen(is);
							if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
								ma.wm.removeView(this);
							}
					}
					super.onTouchEvent(event);
					return true;
				}
			};
			queryUnit.setOrientation(VERTICAL);
			queryUnit.setBackground(new GUI.BorderDrawable());
			unitPx=new GUI.Item(ma);
			unitPx.setText("px（像素）");
			unitPercent=new GUI.Item(ma);
			unitPercent.setText("%（百分数）");
			queryUnit.addView(unitPx,rowlp);
			queryUnit.addView(unitPercent,rowlp);
		}
		void show(){
			wlp.width=ma.wp*4/5;
			wlp.height=-2;
			if(unit==0){
				valueUnit.setText("px");
			}else if(unit==1){
				valueUnit.setText("%");
			}
			if(hor){
				title.setText("横向辅助线");
			}else{
				title.setText("纵向辅助线");
			}
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
			if(view==close){
				ma.wm.removeView(this);
			}else if(view==valueUnit){
				unitlp.width=ma.wp/2;
				unitlp.height=-2;
				ma.wm.addView(queryUnit,unitlp);
			}else if(view==unitPx){
				ma.wm.removeView(queryUnit);
				unit=0;
			}else if(view==unitPercent){
				ma.wm.removeView(queryUnit);
				unit=1;
			}
		}
	}
	byte chart=0;
	class QueryChart extends LinearLayout implements OnClickListener{
		WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
		LinearLayout titlebar;
		TextView title;
		IconViewClose close;
		GUI.Item itContrast;
		GUI.Item itSaturation;
		QueryChart(){
			super(ma);
			setOrientation(VERTICAL);
			setBackground(new GUI.BorderDrawable());
			//wlp.gravity=Gravity.LEFT|Gravity.TOP;
			//wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
			int padding=9;
			int textSize=(int)(13*ma.dp);
			close=new IconViewClose(ma);
			close.setOnClickListener(this);
			title=new TextView(ma);
			title.setText("色彩调节");
			title.setPadding(padding,0,0,0);
			title.setGravity(16);
			title.getPaint().setTextSize(textSize);
			titlebar=new LinearLayout(ma);
			titlebar.setOrientation(HORIZONTAL);
			titlebar.setBackground(new GUI.BorderDrawable());
			titlebar.addView(title,new LayoutParams(0,-1,1f));
			titlebar.addView(close);
			itContrast=new GUI.Item(ma);
			itContrast.setOnClickListener(this);
			itContrast.setText("对比度");
			itSaturation=new GUI.Item(ma);
			itSaturation.setOnClickListener(this);
			itSaturation.setText("饱和度");
			LayoutParams itlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
			addView(itContrast,itlp);
			addView(itSaturation,itlp);
		}
		void show(){
			wlp.width=ma.wp/2;
			wlp.height=-2;
			ma.wm.addView(this,wlp);
		}
		@Override
		public void onClick(View view) {
			if(view==close){
				ma.wm.removeView(this);
			}else if(view==itContrast){
				chart=1;
				ma.wm.removeView(this);
			}else if(view==itSaturation){
				chart=2;
				ma.wm.removeView(this);
			}
		}
		Point touchDown=new Point();
		@Override
		public boolean onTouchEvent(MotionEvent event){
			int x=(int)event.getX();
			int y=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touchDown.x=x;
					touchDown.y=y;
					break;
				case MotionEvent.ACTION_UP:
					Rect visible=new Rect();
					getGlobalVisibleRect(visible);
					int[] is=new int[2];
					getLocationOnScreen(is);
					if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
						ma.wm.removeView(this);
					}
			}
			super.onTouchEvent(event);
			return true;
		}
	}
}
